﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BlogLite.Areas.Admin.Controllers;
using Moq;
using BlogLite.Models;
using BlogLite.Tests.Fakes;
using BlogLite.Models.Database;

namespace BlogLite.Tests.Mocks
{
    public class PostControllerTestContext
    {
        protected readonly PostController controller;
        protected readonly Mock<IBlogLiteRepository> repository;
        protected readonly FakeBlogLiteDatabase fakeDb;

        public PostControllerTestContext()
        {
            controller = new PostController();
            repository = new Mock<IBlogLiteRepository>();
            fakeDb = new FakeBlogLiteDatabase();

            // Setup Mocks
            repository.Setup(m => m.AddPost(It.IsAny<Post>()));
            repository.Setup(m => m.GetPostByPostId(It.IsAny<int>())).Returns((int i) => fakeDb.Posts.SingleOrDefault(t => t.PostId == i));
            repository.Setup(m => m.GetTagByTagId(It.IsAny<int>())).Returns((int i) => fakeDb.Tags.SingleOrDefault(t => t.TagId == i));
            repository.Setup(m => m.GetUserByUsername(It.IsAny<string>())).Returns(fakeDb.Users.FirstOrDefault());
            repository.Setup(m => m.Posts).Returns(fakeDb.Posts);
            repository.Setup(m => m.Save());
            repository.Setup(m => m.Tags).Returns(fakeDb.Tags);            

            // Assign the mock to the controller
            controller.Repository = repository.Object;
        }
    }
}
